<template>
	<view>
		<u-navbar back-icon-color="#fff" background="rgba(0,0,0,0)" ref="nav" is-fixed :border-bottom="false"
			immersive>
			<view :style="menu">
				<u-notice-bar type="none" mode="horizontal" color="#ffffff" :volumeIcon="false" :list="list">
				</u-notice-bar>
			</view>
		</u-navbar>
		<tem-page01 v-if="config.temp.template === 'page01'" :config="config"></tem-page01>
		<tem-page02 v-if="config.temp.template === 'page02'" :config='config'></tem-page02>
		<tem-page03 v-if="config.temp.template === 'page03'" :config='config'></tem-page03>
		<tem-page04 v-if="config.temp.template === 'page04'" :config='config'></tem-page04>
		<tem-page05 v-if="config.temp.template === 'page05'" :config='config'></tem-page05>
		<tem-page06 v-if="config.temp.template === 'page06'" :config='config'></tem-page06>
		<look-prompt></look-prompt>
	</view>
</template>
<script>
	const db = wx.cloud.database()
	export default {
		data() {
			return {
				config: {},
				audio: null,
				menu: {},
				list: []
			}
		},
		onLoad(e) {
			const menu = uni.getMenuButtonBoundingClientRect()
			this.menu = `width: calc(${menu.left}px - 60rpx)`
			// 从预览界面进入
			if (e.id === 'toLook') {
				const config = uni.getStorageSync('config')
				if (config.music) {
					this.audio = uni.createInnerAudioContext()
					playMusic(config.music.info, this.audio)
				}
				this.config = config
				setStroage(config._id, this)
				return;
			}
			// 从其他页面进入
			wx.cloud.callFunction({
				name: "getTemplate",
				data: {
					_id: e.id,
					action: 'getTempitem'
				},
				success: res => {
					const data  = res.result.data
					this.config = data
					if (data.music) {
						this.audio = uni.createInnerAudioContext()
						playMusic(data.music.info, this.audio)
					}
					setStroage(data._id, this)
				}
			})
		},
		onShareAppMessage() {
			const {
				_id
			} = this.config
			return {
				title: "来自朋友的分享~ ~",
				desc: "biu~ biu~ biu~ 请查收",
				path: `/pages/index/look?id=${_id}`
			}
		},
		onUnload() {
			let {
				config
			} = this
			if (config.music) {
				this.audio.stop()
			}
		},
	}
	const playMusic = (music, audio) => {
		audio.src = music;
		audio.autoplay = true;
		audio.loop = true;
	}
	const setStroage = (_id, that) => {
		let clock = uni.getStorageSync('clock')
		if (clock.length !== 0) {
			// 添加到clock
			const newClock = [...clock, _id]
			uni.setStorageSync('clock', newClock)
		} else {
			// 设置一个clock
			const newClock = [_id]
			uni.setStorageSync('clock', newClock)
		}
		// 获取弹幕信息
		wx.cloud.callFunction({
			name: 'setBarrage',
			data: {
				action: 'Get',
				docId: _id
			},
			success: res => {
				console.log();
				res.result.data.forEach(v => {
					that.list.push(v.content)
				})
			}
		})
	}
</script>
